package apps.MyJMRI;

import java.awt.*;

import wiiremotej.WiiRemoteJ;
import wiiremotej.WiiRemote;
import wiiremotej.event.WiiRemoteDiscoveredEvent;
import wiiremotej.event.WiiRemoteDiscoveryListener;

import jmri.util.JmriJFrame;
import lionel.input.*;

public class RemoteFrame extends JmriJFrame implements
		WiiRemoteDiscoveryListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1006063683694532650L;
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(RemoteFrame.class.getName());
	WiiControl m_control;

	public RemoteFrame(String name) {
		super(name);
		getContentPane().setLayout(new FlowLayout());
		try
		{WiiRemoteJ.findRemotes(this);} 
		catch (Exception e)
		{ log.warn("Got exception while enabling wiimote support "+e );}
		
		m_control = new WiiControl(this);
		// JInput devices
		JInput.isAvailable(); JInput.pollAllDevices();
		if ( JInput.getNumDevices()>0)
		{
			for (JInputDevice device: JInput.getDevices() )
				if (m_control.isConnected() == false) 
				{
					//found.addWiiRemoteListener(m_control);
					log.debug("Attaching to main controler "+device);
					m_control.setPad(device);
				}
				else
				{
					log.debug("Setting up another controler "+device);
					JmriJFrame f = new RemoteFrame("Another controler", device);
					f.pack();
					f.setSize(900, 700);        
					Dimension screen = f.getToolkit().getScreenSize();
					Dimension size = f.getSize();
					f.setLocation((screen.width-size.width)/2,(screen.height-size.height)/2);
					f.setVisible(true); 
				}
			JInput.doPolling(true);
		}
	}
	
	public RemoteFrame(String name, WiiRemote control) {
		super(name);
		getContentPane().setLayout(new FlowLayout());
		m_control = new WiiControl(this);
		//control.addWiiRemoteListener(m_control);
		m_control.setRemote(control);		
	}
	
	public RemoteFrame(String name, JInputDevice control) {
		super(name);
		getContentPane().setLayout(new FlowLayout());
		m_control = new WiiControl(this);
		m_control.setPad(control);
	}

	public void paint(Graphics g) {
		super.paint(g);

		if (m_control != null)
		{
			setTitle(m_control.getIDString());
			Image img = m_control.getImage();
			setSize(img.getWidth(null), img.getHeight(null));
			g.drawImage(img, 0, 0, this);
		}
	}

	public void wiiRemoteDiscovered(WiiRemoteDiscoveredEvent evt) {
		WiiRemote found;

		found = evt.getWiiRemote();
		try {

			if (m_control.isConnected() == false) 
			{
				//found.addWiiRemoteListener(m_control);
				log.debug("Attaching to main controler");
				m_control.setRemote(found);
			}
			else
			{
				log.debug("Setting up another controler");
				JmriJFrame f = new RemoteFrame("Another controler", found);
				f.pack();
				f.setSize(900, 700);        
				Dimension screen = f.getToolkit().getScreenSize();
				Dimension size = f.getSize();
				f.setLocation((screen.width-size.width)/2,(screen.height-size.height)/2);
				f.setVisible(true); 
			}
		} catch (Exception e) {
		}
		//WiiRemoteJ.stopFind(); // Only 1 at a time
	}

	public void findFinished(int numberFound) {
		log.debug("Found " + numberFound + " remotes!");
	}

}
